package server;

import java.io.File;
import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

public class RequestProcessor extends Thread {

    private Socket socket;
    private Map<String,HttpServlet> servletMap;
    private Mapper mapper;

    public RequestProcessor(Socket socket, Map<String, HttpServlet> servletMap) {
        this.socket = socket;
        this.servletMap = servletMap;
    }

    public RequestProcessor(Socket socket, Mapper mapper) {
        this.socket = socket;
        this.mapper = mapper;
    }

    @Override
    public void run() {
        try{
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());

            MappingData map = mapper.map(request);


            if (map.getWrapper() != null) {
                map.getWrapper().getHttpServlet().service(request,response);
            }else{
                String url = request.getUrl();
                url = url.substring(url.lastIndexOf("/") + 1);
                response.outputHtml(map.getContext().getAbsolutePath() + File.separator +url);
            }


            /*// 静态资源处理
            if(servletMap.get(request.getUrl()) == null) {
                response.outputHtml(request.getUrl());
            }else{
                // 动态资源servlet请求
                HttpServlet httpServlet = servletMap.get(request.getUrl());
                httpServlet.service(request,response);
            }*/

            socket.close();

        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
